package edu.jd.mainframe;

import edu.jd.aflib.AfColumnLayout;
import edu.jd.homeworkstudent.HomeworkStuService;
import edu.jd.login.CurrentUser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DoHomeworkDialog extends JDialog {
    JButton ok = new JButton("提交");
    private boolean isOK = false;
    JTextArea homeworkTA = new JTextArea(10, 20);
    JLabel submitLab = new JLabel("写入提交：");
    private int hk_id = -1;

    public int getHk_id() {
        return hk_id;
    }

    public void setHk_id(int hk_id) {
        this.hk_id = hk_id;
    }

    private HomeworkStuService homeworkStuService = new HomeworkStuService();

    public DoHomeworkDialog(){}

    public DoHomeworkDialog(JFrame owner, int hk_id){
        super(owner,"提交作业",true);
        this.hk_id = hk_id;
        showing();
    }

    private void showing(){
        this.setSize(400, 300);
        JPanel root = new JPanel();
        this.setContentPane(root);
        root.setLayout(new BorderLayout());
        JPanel main = new JPanel();
        main.setLayout(new AfColumnLayout());
        root.add(main, BorderLayout.CENTER);

        main.add(submitLab);
        main.add(homeworkTA);

        root.add(ok, BorderLayout.PAGE_END);

        ok.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent a){
                if(homeworkTA.getText().length()>0){
                    homeworkStuService.submitHomework(hk_id, CurrentUser.getCurrentUser().getUser_id(), homeworkTA.getText());
                    DoHomeworkDialog.this.setVisible(false);
                    JOptionPane.showMessageDialog(root, "提交成功！");
                    isOK = true;
                }else{
                    JOptionPane.showMessageDialog(root, "提交内容不能为空！");
                }
            }
        });
        this.setVisible(true);
    }

    public boolean isOk(){
        return isOK;
    }
}
